package org.hfy.controller;

import com.baomidou.mybatisplus.core.metadata.IPage;
import org.hfy.common.ApiResponse;
import org.hfy.dto.EmployeeQueryDTO;
import org.hfy.entity.Employee;
import org.hfy.service.EmployeeService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import javax.validation.Valid;
import java.util.Arrays;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * 员工管理Controller
 * 
 * @author hfy
 * @version 1.0
 */
@RestController
@RequestMapping("/employee")
@Validated
public class EmployeeController {

    @Autowired
    private EmployeeService employeeService;

    /**
     * 分页查询员工列表
     * 
     * @param queryDTO 查询条件
     * @return 分页结果
     */
    @GetMapping("/page")
    public ApiResponse<IPage<Employee>> getEmployeePage(EmployeeQueryDTO queryDTO) {
        try {
            IPage<Employee> page = employeeService.getEmployeePage(queryDTO);
            return ApiResponse.success(page);
        } catch (Exception e) {
            return ApiResponse.error("查询员工列表失败：" + e.getMessage());
        }
    }

    /**
     * 根据ID获取员工详情
     * 
     * @param id 员工ID
     * @return 员工信息
     */
    @GetMapping("/{id}")
    public ApiResponse<Employee> getEmployeeById(@PathVariable Long id) {
        try {
            Employee employee = employeeService.getEmployeeById(id);
            if (employee == null) {
                return ApiResponse.error(404, "员工不存在");
            }
            return ApiResponse.success(employee);
        } catch (Exception e) {
            return ApiResponse.error("获取员工详情失败：" + e.getMessage());
        }
    }

    /**
     * 新增员工
     * 
     * @param employee 员工信息
     * @return 操作结果
     */
    @PostMapping
    public ApiResponse<String> addEmployee(@Valid @RequestBody Employee employee) {
        try {
            boolean success = employeeService.addEmployee(employee);
            if (success) {
                return ApiResponse.success("新增员工成功");
            } else {
                return ApiResponse.error("新增员工失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("新增员工失败：" + e.getMessage());
        }
    }

    /**
     * 更新员工
     * 
     * @param id 员工ID
     * @param employee 员工信息
     * @return 操作结果
     */
    @PutMapping("/{id}")
    public ApiResponse<String> updateEmployee(@PathVariable Long id, @Valid @RequestBody Employee employee) {
        try {
            employee.setId(id);
            boolean success = employeeService.updateEmployee(employee);
            if (success) {
                return ApiResponse.success("更新员工成功");
            } else {
                return ApiResponse.error("更新员工失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("更新员工失败：" + e.getMessage());
        }
    }

    /**
     * 删除员工
     * 
     * @param id 员工ID
     * @return 操作结果
     */
    @DeleteMapping("/{id}")
    public ApiResponse<String> deleteEmployee(@PathVariable Long id) {
        try {
            boolean success = employeeService.deleteEmployee(id);
            if (success) {
                return ApiResponse.success("删除员工成功");
            } else {
                return ApiResponse.error("删除员工失败");
            }
        } catch (Exception e) {
            return ApiResponse.error("删除员工失败：" + e.getMessage());
        }
    }

    /**
     * 获取员工身份选项
     * 
     * @return 员工身份列表
     */
    @GetMapping("/roles")
    public ApiResponse<List<Map<String, String>>> getEmployeeRoles() {
        try {
            List<Map<String, String>> roles = Arrays.asList(
                new HashMap<String, String>() {{
                    put("value", "manager");
                    put("label", "业务经理");
                }},
                new HashMap<String, String>() {{
                    put("value", "admin");
                    put("label", "管理员");
                }}
            );
            return ApiResponse.success(roles);
        } catch (Exception e) {
            return ApiResponse.error("获取员工身份失败：" + e.getMessage());
        }
    }

    /**
     * 获取状态选项
     * 
     * @return 状态列表
     */
    @GetMapping("/statuses")
    public ApiResponse<List<Map<String, String>>> getEmployeeStatuses() {
        try {
            List<Map<String, String>> statuses = Arrays.asList(
                new HashMap<String, String>() {{
                    put("value", "active");
                    put("label", "启用");
                }},
                new HashMap<String, String>() {{
                    put("value", "inactive");
                    put("label", "停用");
                }}
            );
            return ApiResponse.success(statuses);
        } catch (Exception e) {
            return ApiResponse.error("获取状态列表失败：" + e.getMessage());
        }
    }
    
    /**
     * 获取所有员工列表（不分页）
     * 
     * @return 所有员工列表
     */
    @GetMapping("/all")
    public ApiResponse<List<Employee>> getAllEmployees() {
        try {
            List<Employee> employees = employeeService.getAllEmployees();
            return ApiResponse.success(employees);
        } catch (Exception e) {
            return ApiResponse.error("获取员工列表失败：" + e.getMessage());
        }
    }
} 